package com.seat.row;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.seat.row.interfaces.OnAlertEventListener;
import com.seat.row.util.AbLogUtil;
import com.seat.row.util.KeyConfig;
import com.seat.row.util.StringUtil;
import com.seat.row.widget.POPConfig;
import com.seat.row.widget.UIAlertDialog;
import com.seat.row.widget.X5WebView;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initAttr();
    }

    private TextView serviceSetting;
    private X5WebView uiWebView;
    private EditText defaultEdit;
    private ProgressBar progressBar;

    private void initAttr() {
        progressBar = findViewById(R.id.progressBar);
        progressBar.setMax(100);
        serviceSetting = findViewById(R.id.serviceSetting);
        serviceSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });
        uiWebView = findViewById(R.id.layoutWebView);
        uiWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int newProgress) {
                super.onProgressChanged(webView, newProgress);
                progressBar.setProgress(newProgress);
                if (newProgress == 100) {
                    progressBar.setVisibility(View.INVISIBLE);
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                }
            }
        });
        defaultEdit = findViewById(R.id.defaultEdit);
        String url = App.getInstance().getACache().getAsString(KeyConfig.lastUrl);
        if (!StringUtil.isEmptyOrNull(url)) {
            defaultEdit.setText(url);
            uiWebView.loadUrl(url);
        }
        findViewById(R.id.goBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = App.getInstance().getACache().getAsString(KeyConfig.lastUrl);
                String inputUrl = defaultEdit.getText().toString().trim();
                if (!StringUtil.isEmptyOrNull(inputUrl)) {
                    if (!inputUrl.equals(url)) {
                        App.getInstance().getACache().put(KeyConfig.lastUrl, inputUrl);
                        uiWebView.loadUrl(inputUrl);
                    }
                }
            }
        });
    }

    private UIAlertDialog ipDialog;

    private void showDialog() {
        ipDialog = new UIAlertDialog(this, POPConfig.ENTER_IP);
        ipDialog.show();
        ipDialog.setOnAlertEventListener(new OnAlertEventListener() {
            @Override
            public void clickEvent(int type) {
                if (type == KeyConfig.ENTER_IP_SURE) {
                    String url = App.getInstance().getACache().getAsString(KeyConfig.lastUrl);
                    String inputUrl = ipDialog.getUrl();
                    if (!StringUtil.isEmptyOrNull(inputUrl)) {
                        if (!inputUrl.equals(url)) {
                            App.getInstance().getACache().put(KeyConfig.lastUrl, inputUrl);
                            uiWebView.loadUrl(inputUrl);
                            defaultEdit.setText(inputUrl);
                        }
                    }
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        if (ipDialog != null) {
            ipDialog.dismiss();
            ipDialog = null;
        }
        super.onDestroy();
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
